﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESystem.Extensions;

namespace ENG.PSSAirbusSoundExtender
{
  public partial class SystemControl : UserControl
  {
    public delegate void ASystemDelegate(SystemControl sender, ASystem s);
    public event ASystemDelegate ChangeRequest;
    public event ASystemDelegate DeleteRequest;
    public event ASystemDelegate SwitchRequest;

    private void TryRaise(ASystemDelegate eventDelegate)
    {
      if (eventDelegate != null)
        eventDelegate(this, aSystem);
    }

    public SystemControl()
    {
      InitializeComponent();
    }

    private ASystem aSystem;
    public void Associate (ASystem system)
    {
      this.aSystem = system;

      aSystem.StatusChanged += new ASystem.StatusChangedHandler(aSystem_StatusChanged);

      ReAssociate();

    }

    void aSystem_StatusChanged(ASystem sender)
    {
      onOffLight1.Value = sender.Status;
    }

    public void ReAssociate()
    {

      lblName.Text = aSystem.Name;

      onOffLight1.Value = aSystem.Status;

      string butts = (aSystem.StartButton != null ? aSystem.StartButton.Name : "") + ";" +
        (aSystem.StopButton != null ? aSystem.StopButton.Name : "");
      lblRange.Text = butts;
    }

    public ASystem GetAssociated()
    {
      return aSystem;
    }

    private void btnChange_Click(object sender, EventArgs e)
    {
      TryRaise(ChangeRequest);
      ReAssociate();
    }    

    private void btnSwitchIsOn_Click(object sender, EventArgs e)
    {
      TryRaise(SwitchRequest);
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
      TryRaise(DeleteRequest);
    }

  }
}
